package BandB.Tool.GPhoneManager.Widget.AndroidBrightnessControl;

import BandB.Tool.GPhoneManager.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
//import android.net.Uri;
import android.provider.Settings.SettingNotFoundException;
import android.widget.RemoteViews;

public class BrightnessControlWidget extends AppWidgetProvider {

//	private String TAG = "AndroidBrightnessControl";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		initialize(context);
	}

	protected void initialize(Context context) {

		AppWidgetManager appManager = AppWidgetManager.getInstance(context);
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.brightnesscontrolwidget);
		Intent intent = new Intent(context, AndroidBrightnessControl.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intent, 0);
		views.setOnClickPendingIntent(R.id.brightness_panel, pendingIntent);
		// views.setTextViewText(R.id.brightnessvalue,
		// getFloatText(getBrightness(context)));
		appManager.updateAppWidget(new ComponentName(context,
				BrightnessControlWidget.class), views);
	}

	// @Override
	// public void onReceive(Context context, Intent intent) {
	// // TODO Auto-generated method stub
	// super.onReceive(context, intent);
	//		
	// Log.w(TAG, "on receive");
	// if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF) ||
	// intent.getAction().equals(Intent.ACTION_CONFIGURATION_CHANGED)){
	// Log.w(TAG, "Got settings action");
	// refreshTextView(context);
	// }
	// }

	public int getBrightness(Context context) {
		int nowBrightnessValue = 0;

		// Uri uri = android.provider.Settings.System
		// .getUriFor("screen_brightness");

		ContentResolver resolver = context.getContentResolver();

		try {
			nowBrightnessValue = android.provider.Settings.System.getInt(
					resolver, "screen_brightness");
		} catch (SettingNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nowBrightnessValue;
	}

	// private void refreshTextView(Context context){

	// RemoteViews views = new
	// RemoteViews(context.getPackageName(),R.layout.brightnesscontrolwidget);
	// views.setTextViewText(R.id.brightnessvalue,
	// getFloatText(getBrightness(context)));
	// }

	protected String getFloatText(int intNowBrightness) {

		String brightnessValueString = "";
		if (intNowBrightness == 255) {
			brightnessValueString = String.valueOf("100%");
		} else {
			Float percentFloat = Float.valueOf(intNowBrightness) / 255.f;
			percentFloat = percentFloat + 0.000001f;
			String percentBrightness = String.valueOf(percentFloat);
			percentBrightness = percentBrightness.substring(percentBrightness
					.indexOf(".") + 1, percentBrightness.indexOf(".") + 3);
			percentBrightness = percentBrightness + "%";
			brightnessValueString = percentBrightness;
		}

		return brightnessValueString;
	}

}
